package com.seed.commons.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 字符编码过滤
 * @author Mr.Yang
 *
 */
public class FilterEncoding implements Filter{
	
	private String encoding = "UTF-8";

	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {

		if(null == req.getCharacterEncoding()) {
			req.setCharacterEncoding(this.encoding) ;
			resp.setContentType("text/html;charset="+encoding) ;
		}
		chain.doFilter(req, resp) ;
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		String encoding = config.getInitParameter("encoding") ;
		
		if(null != encoding && !"".equals(encoding)) {
			this.encoding = encoding ;
		}
	}

}
